local skels = {}

local U = require "packages.utility.utility"

local anmou = fk.CreateSkill{
    name = "xiaobai__anmou",
}
Fk:loadTranslationTable{
    ["xiaobai__anmou"] = "暗谋",
    [":xiaobai__anmou"] = "准备阶段，你可以选择一名其他角色，然后令至多三名角色摸一张牌并与其<a href='delayedPindian'>延时拼点</a>，其只能观看并用牌堆顶的牌拼点。结束阶段，其选择一组延时拼点公开，赢的角色视为对没赢的角色使用一张【杀】，若其没赢，重复此流程。",
    ["#xiaobai__anmou-choose"] = "暗谋：令一名其他角色与至多三名角色进行延时拼点",
    ["#xiaobai__anmou-card"] = "暗谋：你将使用此牌与 %dest 进行延时拼点",
    ["#xiaobai__anmou-target"] = "暗谋：请选择要公开拼点的目标",
    ["#xiaobai__anmou_active"] = "暗谋",
}
anmou:addEffect(fk.EventPhaseStart,{
    can_trigger = function (self, event, target, player, data)
        return target == player and player:hasSkill(anmou.name) and player.phase == Player.Start and
        table.find(player.room.alive_players, function (p)
            return table.find(player.room:getOtherPlayers(p, false), function (q)
                return p:canPindian(q)
            end) ~= nil
        end)
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local success, dat = room:askToUseActiveSkill(player, {
            skill_name = "#xiaobai__anmou_active",
            prompt = "#xiaobai__anmou-choose",
            cancelable = true,
            no_indicate = false,
        })
        if success and dat then
            event:setCostData(self, {tos = dat.targets})
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local targets = event:getCostData(self).tos
        local mark = player:getTableMark("xiaobai__anmou-turn")
        table.insert(mark,targets[1].id)
        room:setPlayerMark(player,"xiaobai__anmou-turn",mark)
        for i = 2, #targets, 1 do
            targets[i]:drawCards(1,anmou.name)
            local top = room:getNCards(1)
            room:viewCards(targets[1],{
                cards = top,
                skill_name = anmou.name,
                prompt = "#xiaobai__anmou-card::"..targets[i].id
            })
            local pindian = U.delayedPindian(targets[1], {targets[i]}, anmou.name, Fk:getCardById(top[1]))
            local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
            if turn_event == nil then return end
            turn_event:addCleaner(function()
                U.delayedPindianCleaner(pindian)
            end)
        end
    end,
})
anmou:addEffect(fk.EventPhaseStart,{
    can_refresh = function (self, event, target, player, data)
        local mark = player:getTableMark("xiaobai__anmou-turn")
        return target == player and player:hasSkill(anmou.name) and player.phase == Player.Finish and #mark > 0
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local mark = player:getTableMark("xiaobai__anmou-turn")
        for _, tpID in ipairs(mark) do
            local tp = room:getPlayerById(tpID)
            local pindian ---@type Utility.DelayedPindianData
            local pindians = {} ---@type table<Utility.DelayedPindianData>
            local targets = {}
            room.logic:getEventsOfScope(GameEvent.DelayedPindian, 3, function(e)
                local dat = e.data
                if dat.reason == anmou.name and dat.from == tp then
                    table.insert(pindians, dat)
                    table.insert(targets, dat.tos[1])
                    return true
                end
            end, Player.HistoryTurn)
            while #targets > 0 do
                local tos = room:askToChoosePlayers(tp,{
                    targets = targets,
                    max_num = 1,
                    min_num = 1,
                    skill_name = anmou.name,
                    prompt = "#xiaobai__anmou-target",
                    cancelable = false,
                })
                table.removeOne(targets,tos[1])
                for _, PindianData in ipairs(pindians) do
                    if PindianData.tos[1] == tos[1] then
                        pindian = PindianData
                    end
                end
                if pindian ~= nil then
                    local from, to = pindian.from, pindian.tos[1]
                    pindian = U.delayedPindianDisplay(pindian)
                    U.delayedPindianCleaner(pindian)
                    local winner = pindian.results[to].winner
                    local loser = from
                    if winner and not winner.dead then
                        if winner == from then
                            loser = to
                            targets = {}
                        end
                        local card = Fk:cloneCard("slash")
                        card.skillName = anmou.name
                        if not winner:prohibitUse(card) and winner:canUseTo(card,loser,{bypass_distances = true,bypass_times = true}) then
                            room:useVirtualCard("slash",nil,winner,{loser},anmou.name,true)
                        end
                    end
                    pindian = nil
                end
            end
        end
    end,
})
table.insert(skels,anmou)

local anmou_active = fk.CreateSkill{
    name = "#xiaobai__anmou_active",
}
anmou_active:addEffect("active",{
    min_target_num = 2,
    max_target_num = 4,
    card_num = 0,
    card_filter = Util.FalseFunc,
    target_filter = function (self, player, to_select, selected)
        if #selected < 4 then
            if #selected == 0 then
                return not to_select:isKongcheng() and to_select ~= player
            else
                return selected[1]:canPindian(to_select)
            end
        end
    end,
    target_tip = function (self, player, to_select, selected, selected_cards, card, selectable, extra_data)
        if not selectable then return end
        if not table.contains(selected, to_select) then
            if #selected == 0 then
                return "拼点发起者"
            else
                return "拼点目标"
            end
        else
            if to_select == selected[1] then
                return "拼点发起者"
            else
                return "拼点目标"
            end
        end
    end,
})
table.insert(skels,anmou_active)

return skels